package com.subaiwang.controller;

import com.alibaba.fastjson.JSON;
import com.subaiwang.pojo.OfficialContent;
import com.subaiwang.pojo.OfficialType;
import com.subaiwang.pojo.Pages;
import com.subaiwang.service.officialWebsite.OfficialTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/official")
public class OfficialTypeController {
    @Resource
    private OfficialTypeService officialTypeService;

    /**
     * 塑胶管理  --分类管理
     *
     * @return
     */
    @RequestMapping("/Product_List2.html")
    public String product_category_add(HttpSession session){
        //设置一个页码
        int pageNo=1;
        Pages pages=new Pages();
        pages.setPageNo(pageNo);
        int count=officialTypeService.getCount3(null,null);
        pages.setPageCount(count);
        officialTypeService.getOfficialType(null,null,pages);

        session.setAttribute("count",count);
        session.setAttribute("list",pages);
        return "Product_List2";
    }

    /**
     * 塑胶管理  --分类管理  分页显示
     * @return
     */
    @RequestMapping(value = "/ProductList2ajax",method = RequestMethod.POST)
    @ResponseBody
    public Object doPost2(HttpSession session,
                          @RequestParam(value = "typename",required = false) String typename,
                          @RequestParam(value = "content",required = false) String content,
                          @RequestParam(value = "pageNo",required = false,defaultValue = "1") int pageNo){
        int pageNoStr=pageNo;
        int pageNo2=1;
        if (pageNoStr==0) {
            pageNo2=1;
        } else {
            pageNo2=pageNoStr;
        }

        Pages pages = new Pages();
        pages.setPageNo(pageNo2);
        int count=officialTypeService.getCount3(typename,content);
        pages.setPageCount(count);
        officialTypeService.getOfficialType(typename,content,pages);
        session.setAttribute("count",count);
        session.removeAttribute("list");
        session.setAttribute("list",pages);
        String jsonarray = JSON.toJSONString(pages);
        return jsonarray;
    }

    /**
     * 分类管理   --跳到添加数据页面
     * @return
     */
    @RequestMapping("/Add_Product_List2.html")
    public String Add_Product_List2()
    {
        return "Add_Product_List2";

    }
    /**
     * 分类管理   --添加数据
     * @return
     */
    @RequestMapping(value = "/Add_Product_List2.html",method = RequestMethod.POST)
    public String doAdd_Product_List2( @RequestParam(value = "content",required = false) String content,
                                      @RequestParam(value = "typename",required = false) String typename,
                                      @RequestParam(value = "contentlink",required = false) String contentlink,
                                      @RequestParam(value = "title",required = false) String title){
        OfficialType officialType=new OfficialType();
        officialType.setContent(content);
        officialType.setContentlink(contentlink);
        officialType.setTitle(title);
        officialType.setType(typename);
        if (officialTypeService.addOfficialType(officialType)){
            return "redirect:/official/Product_List2.html";
        }
        return "Add_Product_List2";
    }


}
